<?php
/**
 * Push报表业务类(Service)
 *   
 * @author wenqiang@6rooms.com
 * @version $Id: Push.php,v 1.0 2013/12/2 15:55:41 gaowenqiang Exp $   
 * @package service.report
 *
 */
class Sr_Service_Report_Push extends Hw_Service_Report_Base
{
	protected $models = array();
	
	/**
	 * 初始化
	 *
	 */
	protected function init($options)
	{
		parent::init($options);

		if (!$this->models) {
			$this->models = array(
				'stat' => $this->modelByStat('push'),
				'report' => $this->model('push'),
			);
		}
	}
	
	/**
	 * 参数校验
	 *
	 * @param array $form 输入参数
	 * @return array
	 *
	 */
	protected function validate($form)
	{
		$errors = array();
		$vars = array();

		while (1) {
			// start end time
			if (!isset($form['start']) || !isset($form['end'])) {
				$errors['start'] = 'invalid start and end time (required)';
				break;
			}

			if (strtotime($form['end']) < strtotime($form['start'])) {
				$errors['end'] = 'Start time can not be greater than the end time';
				break;
			}

			$vars['start'] = $form['start'];
			$vars['end'] = $form['end'];
			
			// prod
			if (!isset($form['prod'])) {
				$errors['prod'] = 'intvalid prod';
				break;
			}
			
			if (!empty($form['prod'])) {
				$vars['prod'] = (int)$form['prod'];
			}
			
			break;
		}

		return array('errors' => $errors, 'vars' => $vars);
	}
	
	/**
	 * 获取条件数组
	 *
	 * @param array $form 表单数据
	 * @param array $fields 列名
	 * @return array
	 *
	 */
	protected function getCond($form, $fields = null)
	{
		$default = array(
				'start',
				'end',
				'prod',
		);

		if (!$fields) {
			$fields = $default;	
		} else {
			$fields = array_merge($fields, $default);
		}

		$cond = array();
		foreach ($fields as $item) {
			$cond[$item] = isset($form[$item]) ? $form[$item] : null;
		}
		
		return $cond;
	}

	/**
	 * 源数据结果计算
	 *
	 * @param string $start 开始时间
	 * @param string $end 结束时间
	 * @param array $input 附加参数
	 * @return array
	 *
	 */
	public function stat($start, $end, $input = array())
	{
		$input = array(
			'start' => $start,
			'end' => $end,
			'prod' => isset($input['prod']) ? $input['prod'] : null
		);

		$output = $this->validate($input);
		if (count($output['errors'])) {
			return false;
		}
		
		// 生成条件
		$cond = $this->getCond($output['vars']);
		// 加载Push Model, 计算
		$result = $this->models['stat']->stat($this->options['type'], $cond['start'], $cond['end'], $cond['prod']);
		if (!$result) {//error
			return false;
		}
		
		// 整理结果
		$rows = array();
		// 时间
		$ts = $cond['start'];
		// 加载Prods Model, $prods = $this->_model_prod->getProdIds();
		$pIds = $this->model('prod')->getProdIds();
		if (is_array($pIds) && !empty($pIds)) {
			foreach ($pIds as $pId) {
				$row = array(
					'prod' => $pId,
					'success_num' => isset($rows[$pId]) ? intval($rows[$pId]['success_num']) : 0,
					'success_stime' => isset($rows[$pId]) ? intval($rows[$pId]['success_stime']) : 0,
					'fail_num' => isset($rows[$pId]) ? intval($rows[$pId]['fail_num']) : 0,
					'fail_stime' => isset($rows[$pId]) ? intval($rows[$pId]['fail_stime']) : 0,
					'pending_num' => isset($rows[$pId]) ? intval($rows[$pId]['pending_num']) : 0,
					'source_ts' => $ts,
				);

				$rows[] = $row;
			}
		}
		
		// 返回数据
		return $rows;
	}
	
	/**
	 * 保存统计结果
	 *
	 * @param mixed(array|string) $data 数据或文件名
	 * @param int $dataSource 数据源类型
	 * @return bool
	 *
	 */
	public function save($data, $dataSource = self::DATA_SOURCE_TYPE_FILE)
	{
		if ($dataSource === self::DATA_SOURCE_TYPE_FILE) {
			$dataFilename = $data;
			return $this->import($dataFilename);
		} else {
			return $this->insert($data);
		}
	}
	
	/**
	 * 从文件导入
	 *
	 * @param mixed(array|string) $data 数据或文件名
	 * @param int $dataSource 数据源类型
	 * @return bool
	 *
	 */
	public function import($dataFilename)
	{
		if (!file_exists($dataFilename)) {
			return false;
		}
		return $this->models['report']->import($dataFilename);
	}
	
	public function insert($data)
	{
		return $this->models['report']->insert($data);
	}

	/**
	 * 获取Push Report列表数据(历史)
	 *
	 * @param string $start 开始时间
	 * @param string $end 结束时间
	 * @param array $prod 产品id
	 * @return array
	 *
	 */
	public function getList($start, $end, $prod = null)
	{
		$input = array(
			'start' => $start,
			'end' => $end,
			'prod' => $prod,
		);

		$output = $this->validate($input);
		if (count($output['errors'])) {
			return false;
		}
		
		// 生成条件
		$cond = $this->getCond($output['vars']);
		
		// get Model
		$rows = $this->models['report']->getList($cond);
		if ($rows === false) {//error
			// write log
			return false;
		}

		return $rows;
	}
	
	/**
	 * 获取Push Report统计数据(历史)
	 *
	 * @param string $start 开始时间
	 * @param string $end 结束时间
	 * @param array $prod 产品id
	 * @return array
	 *
	 */
	public function getTotal($start, $end, $prod = null)
	{
		// 验证表单数
		$input = array(
			'start' => $start,
			'end' => $end,
			'prod' => $prod
		);
		$output = $this->validate($input);
		if (count($output['errors'])) {
			return $output['errors'];
		}

		// 生成条件
		$cond = $this->getCond($output['vars']);
		// get Model
		$rows = $this->models['report']->statTotal($cond);
		if ($rows === false) {//error
			// write log
			return false;
		}

		return $rows;
	}

}